package com.example.administrator.uiautomator.utils;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.view.KeyEvent;

/**
 * Created by Administrator on 2017/7/5.
 */

public class Finder {

    private UiDevice device;
    private final int DEFAULT_TIMEOUT = 5000;

    public Finder(UiDevice device){
        this.device = device;
    }

    public UiObject2 findById(String resourceId){
        device.wait(Until.hasObject(By.res(resourceId)),DEFAULT_TIMEOUT);
        return device.findObject(By.res(resourceId));
    }

    //制定找第几个id
    public UiObject2 findById(String resourceId,int index){
        device.wait(Until.hasObject(By.res(resourceId)),DEFAULT_TIMEOUT);
        return device.findObjects(By.res(resourceId)).get(index);
    }

    public UiObject2 findByText(String text){
        device.wait(Until.hasObject(By.text(text)),DEFAULT_TIMEOUT);
        return device.findObject(By.text(text));
    }

    //指定找 第几个文本
    public UiObject2 findByText(String text,int index){
        device.wait(Until.hasObject(By.text(text)),DEFAULT_TIMEOUT);
        return device.findObjects(By.text(text)).get(index);
    }
    //封装content description定位方法
    public UiObject2 findByContentDesc(String contentDesc){
        device.wait(Until.hasObject(By.desc(contentDesc)),DEFAULT_TIMEOUT);
        return device.findObject(By.desc(contentDesc));
    }

    public void enterPwd(){
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_8);
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_7);
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_6);
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_5);
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_4);
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_3);
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_2);
        device.pressKeyCode(KeyEvent.KEYCODE_BUTTON_1);

    }

    public void goBack(){
        device.pressBack();
    }
}
